<?php

namespace app\admin\controller;

use app\admin\model\Role;
use app\admin\service\RoleService;
use app\admin\validate\RoleValidate;
use common\apidoc\Definitions;
use common\enum\ResultCode;
use hg\apidoc\annotation as Apidoc;
use support\Request;
use support\Response;
use support\Container;
use Tinywan\ExceptionHandler\Exception\BadRequestHttpException;

#[Apidoc\Title("角色管理")]
class RoleController extends BaseController
{
    private RoleService $service;
    protected RoleValidate $validate;

    public function __construct()
    {
        $this->service = Container::get(RoleService::class);
        $this->validate = Container::get(RoleValidate::class);
    }

    #[Apidoc\Title("角色列表")]
    #[Apidoc\Url("admin/role/index")]
    #[Apidoc\Method("GET")]
    #[Apidoc\Query(ref: [Definitions::class, "pagingQueryParam"])]
    #[Apidoc\Query("name", type: "string", desc: "角色名称")]
    #[Apidoc\Query("status", type: "int", desc: "状态:0=禁用,1=启用")]
    #[Apidoc\Returned(ref: [Definitions::class, "pagingReturnParam"])]
    #[Apidoc\Returned("list", type: "array", ref: Role::class, desc: "角色列表")]
    public function index(Request $request): Response
    {
        $params = [
            'page' => $request->get('page', 1),
            'limit' => $request->get('limit', 10),
            'name' => $request->get('name', ''),
            'status' => $request->get('status', ''),
        ];
        if (!$this->validate->check($params, 'index')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->getList($params);
        return $this->success($result);
    }

    #[Apidoc\Title("角色详情")]
    #[Apidoc\Url("admin/role/info")]
    #[Apidoc\Method("GET")]
    #[Apidoc\Query("id", type: "int", require: true, desc: "角色ID")]
    #[Apidoc\Returned(type: "object", ref: Role::class, desc: "角色详情")]
    public function info(Request $request): Response
    {
        $id = $request->get('id');
        if (!$this->validate->check(['id' => $id], 'info')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->getInfo($id);
        return $this->success($result);
    }

    #[Apidoc\Title("添加角色")]
    #[Apidoc\Url("admin/role/create")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("name", type: "string", require: true, desc: "角色名称")]
    #[Apidoc\Param("description", type: "string", require: false, desc: "角色描述")]
    #[Apidoc\Param("status", type: "int", require: false, default: 1, desc: "状态:0=禁用,1=启用")]
    #[Apidoc\Param("sort", type: "int", require: false, default: 0, desc: "排序")]
    #[Apidoc\Returned(type: "object", ref: Role::class, desc: "角色详情")]
    public function create(Request $request): Response
    {
        $data = [
            'name' => $request->post('name'),
            'description' => $request->post('description'),
            'status' => $request->post('status', 1),
            'sort' => $request->post('sort', 0),
        ];
        if (!$this->validate->check($data, 'create')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->create($data);
        return $this->success($result, ResultCode::CREATE_SUCCESS->getDescription());
    }

    #[Apidoc\Title("更新角色")]
    #[Apidoc\Url("admin/role/update")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("id", type: "int", require: true, desc: "角色ID")]
    #[Apidoc\Param("name", type: "string", require: false, desc: "角色名称")]
    #[Apidoc\Param("description", type: "string", require: false, desc: "角色描述")]
    #[Apidoc\Param("status", type: "int", require: false, desc: "状态:0=禁用,1=启用")]
    #[Apidoc\Param("sort", type: "int", require: false, desc: "排序")]
    public function update(Request $request): Response
    {
        $id = $request->post('id');
        $data = [
            'name' => $request->post('name'),
            'description' => $request->post('description'),
            'status' => $request->post('status'),
            'sort' => $request->post('sort'),
        ];
        $data = array_filter(array_merge(['id' => $id], $data), function ($value) {
            return !empty($value);
        });
        if (!$this->validate->check($data, 'update')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->update($id, $data);
        return $this->success($result, ResultCode::UPDATE_SUCCESS->getDescription());
    }

    #[Apidoc\Title("删除角色")]
    #[Apidoc\Url("admin/role/delete")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("id", type: "int", require: true, desc: "角色ID")]
    public function delete(Request $request): Response
    {
        $id = $request->post('id');
        if (!$this->validate->check(['id' => $id], 'delete')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $this->service->delete($id);
        return $this->success(null, ResultCode::DELETE_SUCCESS->getDescription());
    }

    #[Apidoc\Title("角色授权菜单")]
    #[Apidoc\Url("admin/role/grantMenus")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("roleId", type: "int", require: true, desc: "角色ID")]
    #[Apidoc\Param("menuIds", type: "array", require: false, desc: "菜单ID列表")]
    #[Apidoc\Returned("attached", type: "array", desc: "已关联的菜单ID列表")]
    #[Apidoc\Returned("detached", type: "array", desc: "已取消关联的菜单ID列表")]
    public function grantMenus(Request $request): Response
    {
        $roleId = $request->post('roleId');
        $menuIds = $request->post('menuIds', []);
        if (!$this->validate->check(['id' => $roleId], 'grantMenus')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->grantMenus($roleId, $menuIds);
        return $this->success($result, '授权成功');
    }
}